package com.woniuxy.controller;

import com.woniuxy.pojo.InsuranceRecord;
import com.woniuxy.service.InsuranceRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.InsuranceRecordVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class InsuranceRecordController extends BaseController{
    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private InsuranceRecordService insuranceRecordService;

    @ApiOperation(value = "查找保险记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "查找保险记录")
    @GetMapping("/insuranceRecords")
    public DataGridView findInsuranceRecordsByPage(InsuranceRecordVo insuranceRecordVo){
        logger.debug("保险记录："+insuranceRecordVo.toString());
        return insuranceRecordService.findByPage(insuranceRecordVo);
    }

    @ApiOperation(value = "删除保险记录",httpMethod = "DELETE",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "删除保险记录")
    @DeleteMapping(value = "/insuranceRecords/{id}")
    public ResultDto removeInsuranceRecord(@PathVariable("id") int id){
        try {
            insuranceRecordService.removeById(id);
            return ResultDto.INSURANCE_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.INSURANCE_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "批量删除保险记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "批量删除保险记录")
    @GetMapping(value = "insuranceRecords/deleteBatch")
    public ResultDto removeBathInsuranceRecords(@RequestParam("ids[]") Integer[] ids){
        try {
            insuranceRecordService.removeBatch(ids);
            return ResultDto.INSURANCE_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.INSURANCE_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "添加保险记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "添加保险记录")
    @PostMapping(value = "/insuranceRecords")
    public ResultDto addInsuranceRecord(InsuranceRecord insuranceRecord){
        try {
            boolean flag=insuranceRecordService.add(insuranceRecord);
            if (flag) {
                return ResultDto.INSURANCE_RECORD_ADD_SUCCESS;
            } else {
                return ResultDto.INSURANCE_RECORD_ADD_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.INSURANCE_RECORD_ADD_FAIL;
        }
    }

    @ApiOperation(value = "编辑保险记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "编辑保险记录")
    @PostMapping(value = "/insuranceRecords.do")
    public ResultDto updateInsuranceRecord(InsuranceRecord insuranceRecord){
        try {
            boolean flag=insuranceRecordService.update(insuranceRecord);
            if (flag) {
                return ResultDto.INSURANCE_RECORD_UPDATE_SUCCESS;
            } else {
                return ResultDto.INSURANCE_RECORD_UPDATE_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.INSURANCE_RECORD_UPDATE_FAIL;
        }
    }
}
